package cn.hx.mallorder.controller;

import cn.hx.mallorder.entity.Collect;
import cn.hx.mallorder.entity.Comment;
import cn.hx.mallorder.entity.Product;
import cn.hx.mallorder.service.CollectService;
import cn.hx.mallorder.service.ProductService;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class CollectController {
    PublicController publicController = new PublicController();
    @Resource
    CollectService collectService;
    @Resource
    ProductService productService;

    @GetMapping("collect")
    public String collect(Model model, Integer time, HttpServletRequest request) {
        UmsMemberVo userInfo = publicController.getUserInfo(request);
        String memberIP = "1";
        if(userInfo!=null){
            memberIP = userInfo.getId().toString();
        }

        if(time==null){
            time = 0;
        }

        List<Collect> collects = collectService.list(
                new QueryWrapper<Collect>().eq("member_ip", memberIP)
        );

        List<Product> products = new ArrayList<>();
        collects.forEach(o->{
            Product product =  productService.getById(o.getProductId());
            products.add(product);
        });

        List<Product> product2 = new ArrayList<>();
        List<Collect> collect2 = new ArrayList<>();

        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        if (time!=null){
            int index = 0;
            if (time==1){
                instance.set(Calendar.YEAR,instance.get(Calendar.YEAR)-1);
                Date date1 = instance.getTime();
                for (Collect list : collects) {
                    if (list.getCollectTime().after(date1)) {
                        product2.add(products.get(index));
                        collect2.add(list);
                    }
                    index++;
                }
            }
            if (time==2){
                instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)-3);
                Date date1 = instance.getTime();
                for (Collect list : collects) {
                    if (list.getCollectTime().after(date1)) {
                        product2.add(products.get(index));
                        collect2.add(list);
                    }
                    index++;
                }
            }
            if (time==0){ //返回全部
                model.addAttribute("collects",collects);
                model.addAttribute("products",products);
                model.addAttribute("time",time);
                return "collect";
            }
        }

        model.addAttribute("collects",collect2);
        model.addAttribute("products",product2);
        model.addAttribute("time",time);
        System.out.println(products.get(0));
        return "collect";
    }

    @GetMapping("delcollect")
    public String delcollect(Model model,Integer id){
        boolean bl = collectService.removeById(id);
        if (bl) {
            model.addAttribute("msg",200);
        }else{
            model.addAttribute("msg",400);
        }
        return "collect";
    }
}
